/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.design.IAlumnoDAO;
import dao.impl.AlumnoDAO;
import dao.to.AlumnoTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author HAYBERT1
 */
public class ServletNotas extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF8");
        String accion = request.getParameter("accion");
        IAlumnoDAO daoAlu = new AlumnoDAO();
        if (accion ==null){
            request.getSession().setAttribute("bMsg", "Solicitud no recibida");
            response.sendRedirect("mensaje.jsp");
        }else if (accion.equals("QRY")){
            List<AlumnoTO>lista = daoAlu.query();
            request.getSession().setAttribute("bQuery", lista);
            response.sendRedirect("view/menuPrincipal.jsp");
        }else if(accion.equals("INS")){
            String alumno = request.getParameter("txtAlumno");
            String nota1 = request.getParameter("lstNota1");
            String nota2 = request.getParameter("lstNota2");
            String nota3 = request.getParameter("lstNota3");
            String comentario = request.getParameter("txtComentario");
            
            AlumnoTO alu = new AlumnoTO();
            
            alu.setNombres(alumno);
            alu.setNota1(Short.valueOf(nota1));
            alu.setNota2(Short.valueOf(nota2));
            alu.setNota3(Short.valueOf(nota3));
            alu.setComentario(comentario);
            
            String resultado = daoAlu.insert(alu);
            
            if(resultado ==null){
                List<AlumnoTO> lista = daoAlu.query();
                request.getSession().setAttribute("bQuery", lista);
                response.sendRedirect("view/menuPrincipal.jsp");
            }else{
                request.getSession().setAttribute("bMsg", resultado);
                response.sendRedirect("mensaje.jsp");
            }
        }else if(accion.equals("DEL")){
            String idalumno = request.getParameter("txtIdalumno");
            String resultado = daoAlu.delete(Integer.valueOf(idalumno));
            if(resultado == null){
                List<AlumnoTO> lista = daoAlu.query();
                request.getSession().setAttribute("bQuery", lista);
                response.sendRedirect("view/menuPrincipal.jsp");
            }else{
                request.getSession().setAttribute("bMsg", resultado);
                response.sendRedirect("mensaje.jsp");
            }
        }else if(accion.equals("GET")){
            String idalumno = request.getParameter("txtIdalumno");
            AlumnoTO alu = daoAlu.get(Integer.valueOf(idalumno));
            
            request.getSession().setAttribute("bAlumno", alu);
            response.sendRedirect("view/notasUpd.jsp");
        }else if(accion.equals("UPD")){
            String idalumno = request.getParameter("txtIdalumno");
            String alumno = request.getParameter("txtAlumno");
            String nota1 = request.getParameter("lstNota1");
            String nota2 = request.getParameter("lstNota2");
            String nota3 = request.getParameter("lstNota3");
            String comentario = request.getParameter("txtComentario");
            
            AlumnoTO alu = new AlumnoTO();
            
            alu.setIdalumno(Integer.valueOf(idalumno));
            alu.setNombres(alumno);
            alu.setNota1(Short.valueOf(nota1));
            alu.setNota2(Short.valueOf(nota2));
            alu.setNota3(Short.valueOf(nota3));
            alu.setComentario(comentario);
            
            String resultado = daoAlu.update(alu);
            
            if(resultado ==null){
                List<AlumnoTO> lista = daoAlu.query();
                request.getSession().setAttribute("bQuery", lista);
                response.sendRedirect("view/menuPrincipal.jsp");
            }else{
                request.getSession().setAttribute("bMsg", resultado);
                response.sendRedirect("mensaje.jsp");
            }
        }else if(accion.equals("PRO")){
            String idalumno = request.getParameter("txtIdalumno");
            String resultado = daoAlu.promedio(Integer.valueOf(idalumno));
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(resultado);
        }else{
            request.getSession().setAttribute("bMsg", "Solicitud no reconocida");
            response.sendRedirect("mensaje.jsp");
        }
                
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
